﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;

namespace AppMusicStore
{
    public partial class frmMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void album_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowChildForm(new frmAlbum());
        }

        private void receipt_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowChildForm(new frmReceipt());
        }

        private void issue_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowChildForm(new frmIssue());
        }

        private void order_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowChildForm(new frmOrder());
        }

        private void genre_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowChildForm(new frmGenre());
        }

        private void vat_ItemClick(object sender, ItemClickEventArgs e)
        {
            var frm = new frmVAT();
            try
            {
                if (frm.ShowDialog() == DialogResult.Cancel ||
                            this.MdiChildren.Length == 0)
                    return;
            }
            catch (Exception ex)
            {
                Common.ShowErrorBox(ex.Message);
                return;
            }


            var curFrm = this.ActiveMdiChild;
            foreach (var item in this.MdiChildren)
            {
                if (item.Name == "frmIssue" || item.Name == "frmOrder")
                {
                    if (curFrm.Name == "frmIssue")
                    {
                        curFrm = new frmIssue();
                        curFrm.MdiParent = this;
                    }
                    else if (curFrm.Name == "frmOrder")
                    {
                        curFrm = new frmOrder();
                        curFrm.MdiParent = this;
                    }
                    item.Close();
                }
            }
            try
            {
                curFrm.Show();
            }
            catch (Exception ex)
            {
                curFrm.Close();
                Common.ShowErrorBox(ex.Message);
            }
        }

        private void artist_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowChildForm(new frmArtist());
        }


        private void ShowChildForm(Form f)
        {
            bool isExist = false;
            Form curForm = null;
            //lay danh sach form con
            foreach (var frm in this.MdiChildren)
            {
                if (f.Name == frm.Name)
                {
                    isExist = true;
                    curForm = frm;
                    break;
                }
            }
            if (isExist)
                curForm.Close();
            //else
            //{
                f.MdiParent = this;
                //f.WindowState = FormWindowState.Maximized;
                f.FormBorderStyle = FormBorderStyle.None;
                //f.ControlBox = true;
                f.MinimizeBox = false;
                f.MaximizeBox = false;
                try
                {
                    f.Show();
                }
                catch (Exception ex)
                {
                    f.Close();
                    Common.ShowErrorBox(ex.Message);
                }
                
                //frm.Dock = DockStyle.Fill;
            //}
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            ribbonControl1.ShowApplicationButton = DevExpress.Utils.DefaultBoolean.False;
            ribbonControl1.ShowToolbarCustomizeItem = false;
        }

        private void barReport_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowChildForm(new frmReport());
        }

        private void Report_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowChildForm(new frmReport());
        }

        private void About_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                var frm = new frmAboutBox();
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                Common.ShowErrorBox(ex.Message);
            }
            
        }
    }
}